package com.sgb.kjwl.view.ui.activity.self.carMaster;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import android.os.Bundle;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityOfferBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.self.CarriageSelectEntity;
import com.swgk.core.view.entity.DriverAuthDetailEntity;
import com.sgb.kjwl.viewmodel.self.carMakers.OfferModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.customview.ItemInputTextView;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.SharedPreferenceUtil;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;


public class OfferActivity extends AppActivity implements ItemInputTextView.OnWholeItemClickListener {
    @Inject
    OfferModel viewModel;
    ActivityOfferBinding binding;
    private List<CarriageSelectEntity> carModelList;
    private int carModelPosition;
    private List<CarriageSelectEntity> carLengthList;
    private int carLengthPosition;
    private DriverAuthDetailEntity driverAuthDetailEntity;
    private Date today;
    private Calendar selectedDate;
    private String isDriver = "";
    private String goodsSourceNo;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    public static void start(Context context, String goodsSourceNo) {
        Intent intent = new Intent(context, OfferActivity.class);
        intent.putExtra("goodsSourceNo", goodsSourceNo);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_offer);
        binding.setViewModel(viewModel);
        binding.backView.setOnClickListener(this);
        binding.itemTvCarNumber.setOnWholeItemClickListener(this);
        binding.itemTvCarModel.setOnWholeItemClickListener(this);
        binding.itemTvCarLength.setOnWholeItemClickListener(this);
        binding.itemTvTime.setOnWholeItemClickListener(this);
        binding.nextView.setOnClickListener(this);
        binding.itemTvFreightIntention.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        binding.itemTvContactsNumber.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);

    }

    @Override
    protected void init() {
        super.init();
        goodsSourceNo = getIntent().getStringExtra("goodsSourceNo");
        viewModel.offerEntity.get().setGoodsSourceNo(goodsSourceNo);
        selectedDate = Calendar.getInstance();
        isDriver = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.DRIVER_AUTH, "0");
    /*    if ("1".equals(isDriver)) {
            viewModel.driverAuthDetail();
        }*/
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_view:
                finish();
                break;
            case R.id.next_view:
                if (TextUtils.isEmpty(binding.itemTvCarNumber.getContent())) {
                    MToast.showToast(OfferActivity.this, "请输入车牌号");
                    return;
                } else if (TextUtils.isEmpty(binding.itemTvCarModel.getContent())) {
                    MToast.showToast(OfferActivity.this, "请选择车型");
                    return;
                } else if (TextUtils.isEmpty(binding.itemTvTime.getContent())) {
                    MToast.showToast(OfferActivity.this, "请选择可装货时间");
                    return;
                } else if (TextUtils.isEmpty(binding.itemTvFreightIntention.getContent())) {
                    MToast.showToast(OfferActivity.this, "请输入运费意向");
                    return;
                } else if (binding.itemTvFreightIntention.getContent().equals("0")) {
                    MToast.showToast(OfferActivity.this, "运费意向不能为0");
                    return;
                }
                //车牌号
                viewModel.offerEntity.get().setCarNumber(binding.itemTvCarNumber.getContent());
                if (driverAuthDetailEntity != null) {
                    //当前车辆位置 省
                    viewModel.offerEntity.get().setProvinceName(driverAuthDetailEntity.getProvinceName());
                    viewModel.offerEntity.get().setProvinceNo(driverAuthDetailEntity.getProvinceNo());
                    //当前车辆位置 市
                    viewModel.offerEntity.get().setCityName(driverAuthDetailEntity.getCityName());
                    viewModel.offerEntity.get().setCityNo(driverAuthDetailEntity.getCityNo());
                    //当前车辆位置 区
                    viewModel.offerEntity.get().setDistrictName(driverAuthDetailEntity.getDistrictName());
                    viewModel.offerEntity.get().setDistrictNo(driverAuthDetailEntity.getDistrictNo());
                }
                //备注
                viewModel.offerEntity.get().setNote(binding.cfkEdit.getEditContent());
                viewModel.goodsOffer();
                break;
        }
    }

    @Override
    public void OnWholeItemClick(View view) {
        switch (view.getId()) {
            case R.id.item_tv_car_number:
                break;
            case R.id.item_tv_car_model:
                if (carModelList != null) {
                    showCarModelPop();
                } else {
                    viewModel.carType();
                }
                break;
            case R.id.item_tv_car_length:
                if (carLengthList != null) {
                    showCarLengthPop();
                } else {
                    viewModel.carLenType();
                }
                break;
            case R.id.item_tv_load:
                break;
            case R.id.item_tv_time:
                showTimePop();
                break;
        }
    }

    public void getDriverInfo(DriverAuthDetailEntity driverAuthDetailEntity) {
        this.driverAuthDetailEntity = driverAuthDetailEntity;
        //车牌号
        binding.itemTvCarNumber.setContent(driverAuthDetailEntity.getCarNumber());
        //车型
        binding.itemTvCarModel.setContent(driverAuthDetailEntity.getCarTypeName());
        //车辆所在地
        binding.carAddress.setText(driverAuthDetailEntity.getProvinceName() + "-" + driverAuthDetailEntity.getCityName() + "-" + driverAuthDetailEntity.getDistrictName());
    }


    //我的车型
    public void showCarModelPop(List<CarriageSelectEntity> carModelList) {
        this.carModelList = carModelList;
        showCarModelPop();
    }

    private void showCarModelPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(OfferActivity.this, (options1, options2, options3, v) -> {
            String content = carModelList.get(options1).getName();
            binding.itemTvCarModel.setContent(content);
            carModelPosition = options1;
            viewModel.offerEntity.get().setCarTypeNo(carModelList.get(options1).getNo());
        }).build();
        businessOption.setPicker(carModelList);
        businessOption.setSelectOptions(carModelPosition);
        businessOption.show();
    }

    //我的车长
    public void showCarLengthPop(List<CarriageSelectEntity> carLengthList) {
        this.carLengthList = carLengthList;
        showCarLengthPop();
    }

    private void showCarLengthPop() {
        OptionsPickerView businessOption = new OptionsPickerBuilder(OfferActivity.this, (options1, options2, options3, v) -> {
            String content = carLengthList.get(options1).getName();
            binding.itemTvCarLength.setContent(content);
            carLengthPosition = options1;
            viewModel.offerEntity.get().setCarLenNo(carLengthList.get(options1).getNo());
        }).build();
        businessOption.setPicker(carLengthList);
        businessOption.setSelectOptions(carLengthPosition);
        businessOption.show();
    }


    //时间选择器
    public void showTimePop() {
        TimePickerView timePicker = new TimePickerBuilder(OfferActivity.this, (date, v) -> {
            @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(date);
            if (today == null)
                today = new Date();
            int compareTo = today.compareTo(date);
            if (compareTo == 1) {
                Toast.makeText(OfferActivity.this, "请选择小于今天的日期", Toast.LENGTH_SHORT).show();
            } else {
                try {
                    binding.itemTvTime.setContent(format);
                    selectedDate.setTime(date);
                    long times = DateUtils.dateTo10Timestamp(format);
                    MLog.d("time", times + "");
                    viewModel.offerEntity.get().setInTime(format + "");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }).build();
        timePicker.show();
        timePicker.setDate(selectedDate);
    }
}
